import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

// 引入报错信息的路由页面  也就是缺省页(比如：404页面 500页面)
import ErrPage from "./error";
// 引入 首页页脚 的二级路由
import homePage from "./homePage";

const routes = [
    ...ErrPage,
    {
        path: "/",
        redirect: "/index",
    },
    {
        path: "/index",
        name: "Index",
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/Index"),
        children: [{
                path: "/index",
                redirect: "/index/home",
            },
            ...homePage,
        ],
    },
    {
        path: "/login",
        name: "login",
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/login"),
    },
    {
        path: "/house/:id",
        name: "house",
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/House/index.vue"),
    },
    {
        path: "/city",
        name: "city",
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/ChangeCity/index.vue"),
    },
    {
        path: "/map",
        name: "map",
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/map/index.vue"),
    },
    {
        path: "/detail/:id/:query",
        name: "detail",
        component: () =>
            import ( /* webpackChunkName: "about" */ "../views/info/detail/index.vue"),
    },

    {
        path: "/vr",
        name: "vr",
        component: () =>
            import ("../views/vr.vue"),
    },
    {
        path: "/release",
        name: "release",
        component: () =>
            import ("../views/Release/index.vue"),
    },
    {
        //我的二手房
        path: "/myoldhouse",
        name: "myoldhouse",
        component: () =>
            import ("../views/myoldhouse/index.vue"),
        meta: {
            auth: "1001",
        },
    },
    {
        //我的二手房
        path: "/chat/:ids",
        name: "chat",
        component: () =>
            import ("../views/Chat/chatPage.vue"),
    },
    {
        //我的消息列表
        path: "/infoPage",
        name: "infoPage",
        component: () =>
            import ("../views/infoPage/infoPage.vue"),
    },
];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

// 路由拦截
router.beforeEach((to, from, next) => {
    // 获取token值
    const token = localStorage.getItem("token");
    // 判断是否有token 并且当前页不是登录页面
    if (!token && to.path !== "/login" && !to.meta.filter) {
        next("/login");
    } else {
        next();
    }
});

export default router;